package com.hsgene.order.service.impl;

import com.hsgene.order.domain.v1.GeneticInvoice;
import com.hsgene.order.persistence.GeneticInvoiceRepository;
import com.hsgene.order.service.GeneticInvoiceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @projectName: hdas-geneshop-server
 * @package: com.hsgene.order.service.impl
 * @author: maodi
 * @createDate: 2018/10/11 15:19
 * @version: 1.0
 * Copyright: Copyright (c) 2018
 */
@Service
public class GeneticInvoiceServiceImpl implements GeneticInvoiceService {

    @Resource
    GeneticInvoiceRepository geneticInvoiceRepository;

    /**
     * @param personName 个人名称
     * @param userId     用户id
     * @param id         当前发票id
     * @return int
     * @description 根据个人名称和用户id查找数量
     * @author maodi
     * @createDate 2018/10/11 17:23
     */
    @Override
    public int getCountByPersonNameAndUserId(String personName, String userId, String id) {
        return geneticInvoiceRepository.getCountByPersonNameAndUserId(personName, userId, id);
    }

    /**
     * @param companyName 单位名称
     * @param userId      用户id
     * @param id          当前发票id
     * @return int
     * @description 根据单位名称和用户id查找数量
     * @author maodi
     * @createDate 2018/10/11 17:23
     */
    @Override
    public int getCountByCompanyNameAndUserId(String companyName, String userId, String id) {
        return geneticInvoiceRepository.getCountByCompanyNameAndUserId(companyName, userId, id);
    }

    /**
     * @param userId 用户id
     * @return java.util.List<com.hsgene.order.domain.v1.GeneticInvoice>
     * @description 根据用户id获取发票列表
     * @author maodi
     * @createDate 2018/10/11 15:14
     */
    @Override
    public List<GeneticInvoice> getGeneticInvoiceByUserId(String userId) {
        return geneticInvoiceRepository.getGeneticInvoiceByUserId(userId);
    }

    /**
     * @param userId 用户id
     * @param id     发票id
     * @return com.hsgene.order.domain.v1.GeneticInvoice
     * @description 根据id获取发票
     * @author maodi
     * @createDate 2018/10/16 16:36
     */
    @Override
    public GeneticInvoice getGeneticInvoiceById(String userId, String id) {
        return geneticInvoiceRepository.getGeneticInvoiceById(userId, id);
    }

    /**
     * @param geneticInvoice 发票信息实体
     * @return void
     * @description 新增发票信息
     * @author maodi
     * @createDate 2018/10/11 15:14
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void insertGeneticInvoice(GeneticInvoice geneticInvoice) {
        geneticInvoiceRepository.insertGeneticInvoice(geneticInvoice);
        Integer isDefault = geneticInvoice.getIsDefault();
        if (isDefault != null && isDefault == 1) {
            geneticInvoiceRepository.updateOtherGeneticInvoiceToNotDefault(geneticInvoice.getId(), geneticInvoice
                    .getUserId());
        }
    }

    /**
     * @param id     发票id
     * @param userId 用户id
     * @return void
     * @description 修改当前id的发票为默认
     * @author maodi
     * @createDate 2018/10/12 9:28
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void updateGeneticInvoiceToDefault(String id, String userId) {
        geneticInvoiceRepository.updateGeneticInvoiceToDefault(id);
        geneticInvoiceRepository.updateOtherGeneticInvoiceToNotDefault(id, userId);
    }

    /**
     * @param id     当前发票id
     * @param userId 当前用户id
     * @return void
     * @description 修改除当前id外的发票为未默认
     * @author maodi
     * @createDate 2018/10/12 9:14
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void updateOtherGeneticInvoiceToNotDefault(String id, String userId) {
        geneticInvoiceRepository.updateOtherGeneticInvoiceToNotDefault(id, userId);
    }

    /**
     * @param geneticInvoice 发票信息实体
     * @return void
     * @description 修改发票信息
     * @author maodi
     * @createDate 2018/10/11 15:14
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void updateGeneticInvoice(GeneticInvoice geneticInvoice) {
        int type = geneticInvoice.getInvoiceType();
        String id = geneticInvoice.getId();
        geneticInvoiceRepository.updateGeneticInvoice(geneticInvoice);
        if (type == 0) {
            geneticInvoiceRepository.updateToPersonInvoice(id);
        } else {
            geneticInvoiceRepository.updateToCompanyInvoice(id);
        }
        Integer isDefault = geneticInvoice.getIsDefault();
        if (isDefault != null && isDefault == 1) {
            geneticInvoiceRepository.updateOtherGeneticInvoiceToNotDefault(geneticInvoice.getId(), geneticInvoice
                    .getUserId());
        }
    }

    /**
     * @param id 发票id
     * @return void
     * @description 根据发票id删除发票信息
     * @author maodi
     * @createDate 2018/10/11 15:14
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void deleteGeneticInvoiceById(String id) {
        geneticInvoiceRepository.deleteGeneticInvoiceById(id);
    }

}
